﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace EnhancedWebControls
{
    public static class WebControlUtilities
    {

        public static Control RecursiveFindControl(this Control ctrl, string controlId)
        {
            foreach (Control c in ctrl.Controls)
            {
                if (string.Compare(c.ID, controlId, true) == 0)
                    return c;
                else
                {
                    if (c.Controls.Count > 0)
                    {
                        Control rc = RecursiveFindControl(c, controlId);
                        if (rc != null)
                            return rc;
                    }
                }
            }
            return null;
        }
        /// <summary>
        /// Utility Function to convert the Browser version to decimal for version comparisons
        /// </summary>
        /// <param name="browser"></param>
        /// <returns></returns>
        public static double VersionAsDouble(this System.Web.HttpBrowserCapabilities browser)
        {
            double rval = 0;
            double.TryParse(browser.Version, out rval);
            return rval;
        }
        /// <summary>
        /// Utility Function will determine if the browser is Internet Explorer
        /// </summary>
        /// <param name="browser"></param>
        /// <returns>Returns true if the browser is Internet Explorer</returns>
        public static bool IsBrowserIE(this System.Web.HttpBrowserCapabilities browser)
        {
            if (browser.Browser.ToUpper() == "IE")
                return true;
            else
                return false;
        }
        /// <summary>
        /// Utility Function will determine if the browser is Internet Explorer 6 or lower
        /// </summary>
        /// <param name="browser"></param>
        /// <returns>Returns true if the browser is Internet Explorer 6 or lower</returns>
        public static bool IsBrowserIE6orLower(this System.Web.HttpBrowserCapabilities browser)
        {
            if (browser.IsBrowserIE())
                if (browser.VersionAsDouble() < 7.0)
                    return true;
            return false;
        }
        /// <summary>
        /// Utility Function will determine if the browser is Internet Explorer 7
        /// </summary>
        /// <param name="browser"></param>
        /// <returns>Returns true if the browser is Internet Explorer 7</returns>
        public static bool IsBrowserIE7(this System.Web.HttpBrowserCapabilities browser)
        {
            if (browser.IsBrowserIE())
                if (Convert.ToInt32(System.Math.Floor(browser.VersionAsDouble())) == 7)
                    return true;
            return false;
        }
        /// <summary>
        /// Utility Function will determine if the browser is Firefox
        /// </summary>
        /// <param name="browser"></param>
        /// <returns>Returns true if the browser is Firefox</returns>
        public static bool IsBrowserFirefox(this System.Web.HttpBrowserCapabilities browser)
        {
            if (browser.Browser.Contains("Firefox"))
                return true;
            return false;
        }
        public static string EncodeJsString(this string s)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("\"");
            foreach (char c in s)
            {
                switch (c)
                {
                    case '\"':
                        sb.Append("\\\"");
                        break;
                    case '\\':
                        sb.Append("\\\\");
                        break;
                    case '\b':
                        sb.Append("\\b");
                        break;
                    case '\f':
                        sb.Append("\\f");
                        break;
                    case '\n':
                        sb.Append("\\n");
                        break;
                    case '\r':
                        sb.Append("\\r");
                        break;
                    case '\t':
                        sb.Append("\\t");
                        break;
                    default:
                        int i = (int)c;
                        if (i < 32 || i > 127)
                        {
                            sb.AppendFormat("\\u{0:X04}", i);
                        }
                        else
                        {
                            sb.Append(c);
                        }
                        break;
                }
            }
            sb.Append("\"");

            return sb.ToString();
        }
    }
}
